package org.zxyj.manage.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zxyj.core.entity.Pagination;
import org.zxyj.core.errorno.SystemErrorNo;
import org.zxyj.core.exception.ServiceException;
import org.zxyj.core.utils.Common;
import org.zxyj.manage.dao.IBatchDao;
import org.zxyj.manage.entity.Batch;
import org.zxyj.manage.service.IBatchService;
import org.zxyj.system.domain.User;

@Service
public class BatchServiceImpl implements IBatchService{
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private IBatchDao batchDao;
	
	public List<Batch> listEntities(Map<String, Object> criteria) {
		logger.debug("listing entities refer to criteria, param: [{}]", criteria);

		if (null == criteria) {
			criteria = new HashMap<String, Object>();
		}
		return batchDao.find(criteria);
	}

	public Pagination listEntities(Map<String, Object> criteria,Pagination pagination) {
		logger.debug("listing entities refer to criteria and pagination, param: criteria[{}], pagination[{}]", criteria,
				pagination);
		if (null == criteria) {
			criteria = new HashMap<String, Object>();
		}
		criteria.put("pagination", pagination);
		List<Batch> entities = batchDao.find(criteria);
		Long totalCount = batchDao.count(criteria);
		if (null == pagination) {
			pagination = new Pagination();
		}
		pagination.setTotal(totalCount);
		pagination.setDatas(entities);
		return pagination;
	}

	public Batch findEntity(Map<String, Object> criteria) {
		logger.debug("find role refer to criteria, param: criteria[{}]", criteria);

		if (null == criteria) {
			// 参数错误
			return null;
		}
		return batchDao.findById(criteria);
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void saveOrUpdateEntity(Batch entity) {
		logger.debug("persistenting entity to DB, param: entity[{}]", entity);
		// 参数校验
		if (null == entity) {
			logger.warn("persistenting entity to DB, param entity is null.");
			throw new ServiceException(SystemErrorNo.PARAMETER_IS_NULL);
		}

		User user = (User) Common.findUserSession();
		entity.setCreator(user);
		
		if (null == entity.getId() || StringUtils.isEmpty(entity.getId())) {
			batchDao.insert(entity);
			logger.debug("entity not exist in DB, then save new entity to DB, entity: {}", entity.getId());
		} else {
			Map<String, Object> criteria = new HashMap<String, Object>();
			criteria.put("id", entity.getId());
			Batch tempEntity = this.findEntity(criteria);
			if (null == tempEntity) {
				batchDao.insert(entity);
				logger.debug("entity not exist in DB, then save new entity to DB, entity: {}", entity.getId());
			} else {
				batchDao.update(entity);
				logger.debug("entity exist in DB, then update entity info, entity: {}", entity.getId());
			}
		}
	}
	
	@Transactional(rollbackFor = Exception.class)
	public void deleteEntity(Map<String, Object> criteria) {
		logger.debug("deleting entity from DB, param: criteria[{}]", criteria);

		if (!MapUtils.isEmpty(criteria)) {
			batchDao.delete(criteria);
		}
	}
}
